﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using LiuYan.Entity;
using LiuYan.Service;

namespace LiuYan.Web.Areas.Admin.Controllers
{
    public class GoodSortController :BaseController
    {
        //
        // GET: /Admin/GoodSort/

        public ActionResult Index()
        {
            ViewData["sort"] = new SortService().GetList();
            return View();
        }
        public ActionResult Window(int id)
        {
            ViewData["id"] = id;
            return View();
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelSort(int id)
        {
            return new SortService().Del(id);
        }
        /// <summary>
        /// 分类编辑
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public int Edit(int id, string sort,int order,string des,string img)
        {
            TB_Sort entity = new TB_Sort();
            entity.Id = id;
            entity.Name = sort;
            entity.Order = order;
            entity.Des = des;
            entity.Img = img;
            return new SortService().Edit(entity);
        }
        /// <summary>
        /// 获取一条分类数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetOnetData(int id)
        {
           return  JsonConvert.SerializeObject(new BaseService().GetOneData(id, "TB_Sort"));
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        public string UploadImg()
        {
            HttpFileCollection file = System.Web.HttpContext.Current.Request.Files;
            int num = file.Count;
            string imgPath = "";
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string fileName = file[0].FileName.Split('\\').Last();
            imgPath = "/Content/sortimg/" + Convert.ToInt64(ts.TotalSeconds) + "." + fileName.Split('.').Last();
            string path = Server.MapPath(imgPath);
            file[0].SaveAs(path);
            return "{'status':'1','path':'" + imgPath + "'}";
        }
    }
}
